<?php

declare (strict_types = 1);

namespace app\common\model;

use cores\BaseModel;

/**
 * 商品分类模型
 * Class Category
 * @package app\common\model
 */
class Category extends BaseModel
{
    // 定义表名
    protected $name = 'category';

    // 定义主键
    protected $pk = 'category_id';

    /**
     * 分类图片
     * @return \think\model\relation\HasOne
     */
    public function image()
    {
        return $this->hasOne('UploadFile', 'file_id', 'image_id')->bind(['image_url' => 'preview_url']);
    }

    /**
     * 分类详情
     * @param int|array $where
     * @param array $with
     * @return array|null|static
     */
    public static function detail($where, array $with = [])
    {
        return static::get($where, $with);
    }

    /**
     * 获取列表记录
     * @param array $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(array $param = []): array
    {
        $list = $this->getAll($param);
        if(!empty($list)){
            foreach ($list as &$item){
                //todo  兼容文件分组 group_id  现在共用一个分类
                $item['group_id'] = $item['category_id'];
            }
        }
        return $this->getTreeData($list);
    }

    /**
     * 获取所有记录
     * @param array $param
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getAll(array $param = [])
    {
        // 默认查询参数
        $params = $this->setQueryDefaultValue($param, [
            'status' => -1  // 状态(1显示 0隐藏 -1全部)
        ]);
        // 设置检索条件
        $filter = [];
        $filter[] = ['is_batch', '=', 0];
        $params['status'] > -1 && $filter[] = ['status', '=', $params['status']];
        // 查询列表数据
        return $this->with(['image'])
            ->where($filter)
            ->order(['sort', 'created_at'])
            ->select();
    }

    /**
     * 获取树状列表
     * @param $list
     * @param int $parentId
     * @return array
     */
    private function getTreeData($list, int $parentId = 0,$level = 0)
    {
        $data = [];
        foreach ($list as $key => $item) {
            if ($item['parent_id'] == $parentId) {
                $item['level'] = $level;
                if($level == 0){
                    $item['ip'] = $item['name'] ?? '';
                }elseif ($level == 1){
                    $item['series'] = $item['name'] ?? '';
                }
                $children = $this->getTreeData($list, $item['category_id'],$level + 1);
                !empty($children) && $item['children'] = $children;
                $data[] = $item;
                unset($list[$key]);
            }
        }
        return $data;
    }

    /**获取子集分类ID
     * @param $category_id
     * @param $list
     * @return array
     */
    public function getTreeIds($category_id,$list):array
    {
        static $sonIds = [];
        foreach ($list as $item) {
            if ($item['parent_id'] == $category_id && $item['parent_id'] > 0) {
                $sonIds[] = $item['category_id'];
                $this->getTreeIds($item['category_id'], $list);
            }
        }
        return $sonIds;
    }

}
